package com.pphh.demo;

import com.pphh.demo.model.UserInfoDTO;
import com.pphh.demo.service.UserAService;
import com.pphh.demo.service.UserBService;
import com.pphh.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/v1/test")
public class TestController {

    @Autowired
    UserService userService;

    @Autowired
    UserAService userAService;

    @Autowired
    UserBService userBService;

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String hello() throws Exception {
        return "hello,world";
    }

    @RequestMapping(value = "/user/{userId}", method = RequestMethod.GET)
    public UserInfoDTO getUserInfo(@PathVariable Long userId) throws Exception {
        return userService.getByUserId(userId);
    }

    @RequestMapping(value = "/user", method = RequestMethod.POST)
    public UserInfoDTO addUserInfo(@RequestBody UserInfoDTO user) throws Exception {
        return userService.insertUser(user);
    }

    @RequestMapping(value = "/user/internal", method = RequestMethod.POST)
    public UserInfoDTO addUserInfoTx(@RequestBody UserInfoDTO user) throws Exception {
        return userService.insertUserNTx(user);
    }

    @RequestMapping(value = "/user/nested", method = RequestMethod.POST)
    public UserInfoDTO addUserInfoDTx(@RequestBody UserInfoDTO user) throws Exception {
        return userAService.insertUser(user);
    }

    @RequestMapping(value = "/user/{userId}", method = RequestMethod.DELETE)
    public String removeUserInfo(@PathVariable Long userId) throws Exception {
        userService.remove(userId);
        return "deleted";
    }

}
